#ifndef NODEMANAGERPANEL_H
#define NODEMANAGERPANEL_H

#include "common.h"
#include <wx/dataview.h>
#include "signal.hpp"

class NodeManagerPanel;

class ParamEditPanel : public wxPanel
{
public:
    ParamEditPanel(wxWindow* parent);
    ~ParamEditPanel();

    // 设置当前编辑结点
    inline void setBaseNode(WBaseNode *pBasenode)
    {
        mBasenode=pBasenode;
    }
    // 挂接道岔改变信号槽
    void sig_ProcessItemChanged(NodeManagerPanel *pNodeManagerPanel);
    // 得到当前编辑结点
    inline const WBaseNode* getBaseNode(void) { return mBasenode; }
    // 获取所有参数数据
    std::map<wxString, wxString> GetData() const;
    // 设置参数数据
    void SetData(const std::map<wxString, wxString>& data);
    // 重置所有参数
    void resetAllData(void);

private:
    void InitUI();

    void OnNameTextChanged(wxCommandEvent& event);
    void OnCoordTextChanged(wxCommandEvent& event);
    void OnSwitchStateChanged(wxCommandEvent& event);
    void OnNameTextKillFocus(wxFocusEvent& event);
    void OnCoordTextKillFocus(wxFocusEvent& event);
    void OnSwitchStateKillFocus(wxFocusEvent& event);
    void OnNameTextEnter(wxCommandEvent& event);
    void OnCoordTextEnter(wxCommandEvent& event);
    void OnSwitchStateTextEnter(wxCommandEvent& event);

    void OnDeviceIDChanged(wxCommandEvent& event);
    void OnDeviceIDKillFocus(wxFocusEvent& event);
    void OnDeviceIDEnter(wxCommandEvent& event);

    void OnNodeTypeComboBoxSelected(wxCommandEvent& event);
    void OnSignalDirComboBoxSelected(wxCommandEvent& event);
    void OnSignalRouteComboBoxSelected(wxCommandEvent& event);

private:
    WBaseNode *mBasenode;
    sigslot::signal4<WBaseNode*,int,wxString,wxString> sig_item_change;
    std::map<wxString, wxString> m_lastChangedData;

    // 控件指针
    wxStaticText* m_nodeTypeLabel;
    wxComboBox* m_nodeTypeCombo;

    wxStaticText* m_nodeNameLabel;
    wxTextCtrl* m_nodeNameText;
    bool m_NameTextEditFinished;

    wxStaticText* m_deviceidLabel;
    wxTextCtrl* m_deviceidText;
    bool m_deviceidTextEditFinished;

    wxStaticText* m_coordLabel;
    wxTextCtrl* m_coordText;
    bool m_coordTextEditFinished;

    wxStaticText* m_signalDirLabel;
    wxComboBox* m_signalDirCombo;

    wxStaticText* m_signalRouteLabel;
    wxComboBox* m_signalRouteCombo;

    wxStaticText* m_switchStateLabel;
    wxTextCtrl* m_switchStateText;
    bool m_switchStateTextEditFinished;
};

class MountPointsPanel : public wxPanel
{
public:
    MountPointsPanel(wxWindow *parent);
    ~MountPointsPanel();

    // 设置当前编辑结点
    inline void setBaseNode(WBaseNode *pBasenode) { mBasenode=pBasenode; }
    // 得到当前编辑结点
    inline const WBaseNode* getBaseNode(void) { return mBasenode; }
    void clearAllData(void);
    void addData(const wxVector<wxVariant> data);
    // 重置所有参数
    void resetAllData(void);
    // 重新加载所有的结点
    void reloadAllNodes(void);

private:
    void OnEditingDone(wxDataViewEvent& event);
    void OnRightClick(wxDataViewEvent& event);

    void OnDeleteMountPoint(wxCommandEvent& event);
    void OnAddNewMountPoint(wxCommandEvent& event);

private:
    WBaseNode *mBasenode;

    wxDataViewCtrl* m_dvc;
    wxDataViewListStore* m_store;
    wxDataViewColumn* m_choicescolumn;
};

class NodeManagerPanel : public sigslot::has_slots<>,
                         public wxPanel
{
public:
    NodeManagerPanel(wxWindow *parent);
    ~NodeManagerPanel();

    inline WBaseNode* getBaseNode(void) { return m_BaseNode; }
    void addBaseNode(WBaseNode* pbasenode);
    void addData(const wxVector<wxVariant> data);
    void slot_ProcessItemChanged(WBaseNode *pBasenode,int itemindex,wxString olddata,wxString newdata);

private:
    void OnItemActivated(wxDataViewEvent& event);
    void OnSelectionChanged(wxDataViewEvent& event);
    void OnDeleteItem(wxCommandEvent& event);
    void OnRightClick(wxDataViewEvent& event);

    void OnAddNewMountNode(wxCommandEvent& event);
    void OnAddNewSignalLamp(wxCommandEvent& event);
    void OnAddNewSwitch(wxCommandEvent& event);

private:
    WBaseNode* m_BaseNode;
    wxDataViewCtrl* m_dvc;
    wxDataViewListStore* m_store;
    ParamEditPanel *m_ParamEditPanel;
    MountPointsPanel *m_MountPointsPanel;
};

#endif // NODEMANAGERPANEL_H
